<?php

namespace app\api\controller\admin;

use app\common\controller\Api;
use think\Db;

/**
 * 软件列表接口
 */
class Logcardrefill extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function index()
    {
        $this->success('请求成功66555');
    }

    /**
     * 查看
     */
    public function gindex()
    {
//        halt(5);
        $params = $this->request->param();

        $action =$params['action'];
        if($action=='apiindex'){
            $data= self::apiindex();
            return $data;
        }elseif ($action=='apiadd'){
            self::apiadd();
        }elseif ($action=='apiedit'){
            self::apiedit();
        }elseif ($action=='apidel'){
            self::apidel();
        }



    }

    /**
     * 查看
     */
    public function apiindex()
    {
        $params = $this->request->param();
        $pageSize =$params['pageSize'];
        $pageNum =$params['pageNum'];
        $name =isset($params['name']) ? $params['name'] : '';
        $sort=isset($params['sort']) ? $params['sort'] : 'id';
        $order=isset($params['order']) ? $params['order'] : 'desc';
        $systemFlag=isset($params['systemFlag']) ? $params['systemFlag'] : '';
        $where=[];
        if ($systemFlag){
            $where['id']  = array('like',  '%'.$systemFlag."%");
        }
        if ($name){
            $where['name']  = array('like',  '%'.$name."%");
        }
//        $pageNum=1;
//        $pageSize=1;
//        $sort='id';
//        $order='desc';
//halt($where);
        $pageNum=($pageNum-1)*$pageSize;
        $list = Db::table('fa_g_logcardrefill')
            ->where($where)
//            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit($pageNum,$pageSize)
            ->select();
//        halt($list);
        $total= Db::table('fa_g_logcardrefill')
            ->where($where)
//            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit(1,2)
            ->count();

//        foreach ($list as $k => &$v) {
////            dump($v['forcelist']);
//            $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
//            $v['groups'] = implode(',', array_keys($groups));
//            $v['groups_text'] = implode(',', array_values($groups));
//        }
//        unset($v);
        $result = array("total" => $total, "rows" => $list);
        return json($result);

    }
    public function apiadd()
    {

        $post = $this->request->post();
        unset($post['action']);
        $post['createtime']=time();
        $data= self::apikm($post);
        $list=Db::table('fa_g_logcardrefill')->insertAll($data['arr']);
//halt($list);
        $this->success('成功',$data['km']);

    }
    public function apikm($post){
        $arr=[];
        $km=[];
        for ($i=0; $i<$post['zknum']; $i++)
        {
            $arra[$i]=$post['day'];
            $arr[$i]['name']= mt_rand(999, 9999) .'-'. mt_rand(9, 99) .chr(rand(97,122)). mt_rand(0, 9).'-'.chr(rand(97,122)). mt_rand(0, 9).chr(rand(97,122)). mt_rand(0, 9) .'-'.  chr(rand(97,122)).sprintf('%02d', rand(0, 99999)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122));
            $arr[$i]['day']=$post['day'];
            $arr[$i]['dl']=$post['dl'];
            $arr[$i]['remark']=$post['remark'];
            $arr[$i]['software']=$post['software'];
            $arr[$i]['zktime']= date("Y-m-d H:i:s");
            $arr[$i]['createtime']= time();
            $arr[$i]['ph']= date("YmdHis");
            $km[]=$arr[$i]['name'];
        }
        $data['arr']=$arr;
        $data['km']=$km;
//        halt($data);
         return $data;
    }
    public function apiedit()
    {
        $post = $this->request->post();
        unset($post['action']);
        $post['updatetime']=time();
        $list=Db::table('fa_g_logcardrefill')->update($post);
        $this->success($list);

    }
    public function apidel()
    {
        $post = $this->request->post();
        unset($post['action']);
        $list=Db::table('fa_g_logcardrefill')->delete($post);
        $this->success($list);

    }
}
